package com.polaris.live.adapter.party

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.databinding.AdapterRoomManagementBinding
import com.polaris.live.ui.party.bean.ManagementBean

/**
 *  房间管理adapter
 * @author Created by 天晴 on 2024/2/27 19:39
 * @since 1.0.0
 **/
class RoomManagementAdapter :
    BaseQuickAdapter<ManagementBean, RoomManagementAdapter.RoomManagementHolder>(0) {

    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): RoomManagementHolder {
        return RoomManagementHolder(
            AdapterRoomManagementBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun convert(holder: RoomManagementHolder, item: ManagementBean) {
        holder.binding.apply {

            viewRed.visibility = if (item.red) View.VISIBLE else View.GONE

            when (item.operateType) {
                1 -> {
                    item.resourcesOpen?.let {
                        settingImg.setImageResource(it)
                    }
                    nameTv.text = item.name
                }

                2 -> {
                    if (item.state) {
                        item.resourcesOpen?.let {
                            settingImg.setImageResource(it)
                        }
                        nameTv.text = item.name
                    } else {
                        item.resourcesClose?.let {
                            settingImg.setImageResource(it)
                        }
                        nameTv.text = item.closeName ?: item.name
                    }
                }

                3 -> {
                    switchBtn.visibility = View.VISIBLE
                    switchBtn.isChecked = item.state
                    item.resourcesOpen?.let {
                        settingImg.setImageResource(it)
                    }
                    if (item.state) {
                        nameTv.text = item.closeName ?: item.name
                    } else {
                        nameTv.text = item.name

                    }
                    //
                    switchBtn.setOnCheckedChangeListener { buttonView, isSelected ->
                        // 禁用默认的状态变更
                        if (buttonView.isPressed) {
                            switchBtn.isChecked = !isSelected
                            item.back?.invoke(isSelected, holder.absoluteAdapterPosition)
                        }
                    }
                }
            }
        }
    }

    inner class RoomManagementHolder(val binding: AdapterRoomManagementBinding) :
        BaseViewHolder(binding.root) {
    }

}